#include <game/GameApplication.h>
#include <game/Root.h>
#include <game/services/ApplicationService.h>
#include <input/Manager.h>

ARELLO_GAME_NAMESPACE_DECL_START

GameApplication::GameApplication(const std::string & caption, const std::string & cmd_line)
	: Application(caption, cmd_line)
{ }

GameApplication::GameApplication(const std::string & caption, int sx, int sy, int resizeable)
	: Application(caption, sx, sy, resizeable)
{ }

void GameApplication::on_loading_start()
{
	if (!this->GetRoot())
		return;

	services::ApplicationService* appService = this->GetRoot()->Query<services::ApplicationService>();

	if (!appService)
		return;

	appService->InvokeLoadingStarted();
}

void GameApplication::on_loading_finish()
{
	if (!this->GetRoot())
		return;

	services::ApplicationService* appService = this->GetRoot()->Query<services::ApplicationService>();

	if (!appService)
		return;

	appService->InvokeLoadingFinished();
}

bool GameApplication::LifeCycle(time_float dt)
{
	input::Manager::LifeCycle();
	if(is_loading_now())
		return lib3d::Application::LifeCycle(dt);

	if (dt > 0.1)
		dt = static_cast<time_float>(0.1);

	GetRoot()->LifeCycle(dt);

	return lib3d::Application::LifeCycle(dt);
}

bool GameApplication::render()
{
	if (this->hardware->start_frame())
	{
		GetRoot()->render(hardware);
		this->render_cursor(this->hardware);
		hardware->flip();
	}

	return true;
}

ARELLO_GAME_NAMESPACE_DECL_END